<?php
require_once('../classes/smarty_setup.php');
require('../classes/mofachi.Blog.php');
$smarty = new Smarty_mofachi();

@ $act = $_REQUEST[ "act" ];
switch( $act ) {
	case "add": addBlog(); break;
	case "remove": removeBlog(); break;
	case "get": getBlog( $smarty ); break;
	case "update": updateBlog(); break;
	case "vote": voteBlog(); break;
}
$smarty->assign( 'act', $act );

listBlog( $smarty );
$smarty->display('t06_blog.htm');

function listBlog( $smarty ) {
	$result = Blog::getRecent( 0, 10 );
	$smarty->assign( 'res', $result );
}
function addBlog() {
	@ $visible = $_POST[ 'visible' ];
	@ $title = $_POST[ 'title' ];
	@ $content = $_POST[ 'content' ];
	if( !$content || !$title )
		return;
	
	$blog = new Blog();
	$blog->visible = $visible;
	$blog->title = $title;
	$blog->content = $content;
	$blog->add();
}
function removeBlog() {
	@ $delist = $_POST[ "delist" ];
	if( !$delist )
		return;

	$id_array = explode( ",", $delist );
	foreach( $id_array as $key => $value ) {
		$blog = new Blog();
		$blog->blog_id = $value;
		$blog->remove();
	}
}
function getBlog( $smarty ) {	
	@ $bid = $_GET[ "bid" ];
	if( !$bid )
		return;
	$blog = new Blog();
	$blog->blog_id = $bid;
	$blog->loadById();
	$smarty->assign( 'a', $blog );
}
function updateBlog() {
	@ $bid = $_POST[ 'bid' ];
	@ $title = $_POST[ 'title' ];
	@ $content = $_POST[ 'content' ];
	if( !$content || !$title )
		return;
	
	$blog = new Blog();
	$blog->blog_id = $bid;
	$blog->loadById();
	$blog->title = $title;
	$blog->content = $content;
	$blog->update();
}
function voteBlog() {
	@ $bid = $_GET[ 'bid' ];
	@ $yn = $_GET[ 'yn' ];
	if( !$bid || $yn == null )
		return;

	$blog = new Blog();
	$blog->blog_id = $bid;
	if( $yn )
		$blog->voteYes();
	else
		$blog->voteNo();
}
?>